<script setup lang="ts">
import { ref, watch } from 'vue'
import NavBar from '@/components/NavBar.vue'
import MyList from '@/pages/my/components/MyList.vue'
import FriendRequest from '@/pages/my/components/FriendRequest.vue'
import NoData from '@/components/NoData/NoData.vue'
// 获取数据
import { getInterchangeList } from '@/services/my/MyInterchange/MyInterchange'
import { onLoad } from '@dcloudio/uni-app'

// 获取到用户的ID
const userID = uni.getStorageSync('userInfo').id

let navList = ref<string[]>(['我发起的', '我收到的', '互换成功'])
let current = ref<number>(0)
//我发送的数据
let data = ref([])
// 我收到的数据
let receive = ref([])
// 互换成功的
let success = ref([])
// 请求函数
const getInterchange = async () => {
  const dataRes: any = await getInterchangeList(userID, 0)
  const receiveRes: any = await getInterchangeList(userID, 1)
  const successRes: any = await getInterchangeList(userID, 2)
  data.value = dataRes?.data?.result
  receive.value = receiveRes?.data?.result
  success.value = successRes?.data?.result
}

onLoad(() => {
  getInterchange()
})
</script>
<template>
  <!-- 我的互换 -->
  <NavBar
    :navList="navList"
    @change="
      (index: number) => {
        current = index
      }
    "
  />
  <view class="main">
    <view v-if="current === 0">
      <template v-if="data.length > 0">
        <MyList :data="data" />
      </template>
      <NoData v-else />
    </view>
    <view v-else-if="current === 1">
      <template v-if="receive.length > 0">
        <FriendRequest :data="receive" @getInterchange="getInterchange" />
      </template>
      <NoData v-else />
    </view>
    <view v-else>
      <template v-if="success.length > 0">
        <MyList :data="success" />
      </template>
      <NoData v-else />
    </view>
  </view>
</template>
<style lang="scss" scoped>
.main {
  width: 100%;
  height: calc(100vh - 100px);
}
</style>
